﻿Public Class SearchProjectsForm
    Public idSelected As Integer

    Private Sub SearchProjectsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GenericAdministrator.loadGrid(Me.dgvSearchProjects, Me.bdnSearchProjects, ProjectAdministrator.searchSummary)

        Dim stateScope As StateScope = New StateScope(stateScope.STATE_SCOPE_PROJECT)
        Dim state As State = New State()
        state.stateScopeProperty = stateScope

        GenericAdministrator.loadComboBox(Me.cboProjectState, StateAdministrator.searchDataByFilter(state), "id", "name")
        GenericAdministrator.loadComboBox(Me.cboProjectResponsible, ResourceAdministrator.search("id, (surname || ', ' || firstname) as name"), "id", "name")
        Me.setDataGridViewHeaders()
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Dim state As New State
        If Not Me.chkAllStates.Checked Then
            state.idProperty = Me.cboProjectState.SelectedValue
        End If

        Dim projectResponsible As New Resource
        If Not Me.chkAllResponsibles.Checked Then
            projectResponsible.idProperty = Me.cboProjectResponsible.SelectedValue
        End If

        Dim project As New Project(Nothing, Me.txtProjectName.Text, Nothing, Nothing, Nothing, Nothing, state, projectResponsible)
        GenericAdministrator.loadGrid(Me.dgvSearchProjects, Me.bdnSearchProjects, ProjectAdministrator.searchDataByFiltersSummary(project))
    End Sub

    Private Sub bdnEditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bdnEditButton.Click
        If Me.dgvSearchProjects.CurrentRow IsNot Nothing Then
            idSelected = Me.dgvSearchProjects.CurrentRow.Cells(0).Value
        Else
            idSelected = Me.dgvSearchProjects.Rows(0).Cells(0).Value
        End If

        'If Me.dgvSearchProjects.CurrentRow IsNot Nothing Then
        ' idSelected = Me.dgvSearchProjects.CurrentRow.Cells(0).Value
        Dim project As Project = ProjectAdministrator.searchDataById(idSelected)
        'If project.getProjectState.isCanUpdate() = True Then
        Me.Close()
        Dim projectForm As New ProjectForm(project)
        projectForm.Show()
        'End If
        'Else
        'MsgBox("Debe seleccionar un registro.")
        'End If
    End Sub

    Private Sub setDataGridViewHeaders()
        Me.dgvSearchProjects.Columns(0).HeaderText = "Id"
        Me.dgvSearchProjects.Columns(0).Visible = False
        Me.dgvSearchProjects.Columns(1).HeaderText = "Nombre"
        Me.dgvSearchProjects.Columns(2).HeaderText = "Descripcion"
        Me.dgvSearchProjects.Columns(3).HeaderText = "Fecha Estimada Inicio"
        Me.dgvSearchProjects.Columns(4).HeaderText = "Fecha Estimada Fin"
        Me.dgvSearchProjects.Columns(5).HeaderText = "Estado"
        Me.dgvSearchProjects.Columns(6).HeaderText = "Nombre Responsable"
        Me.dgvSearchProjects.Columns(7).HeaderText = "Apellido Responsable"
    End Sub
End Class